package com.gzs.learn.payment.service.api.impl;

import com.gzs.learn.common.util.HttpUtil.Response;
import com.gzs.learn.payment.inf.exception.PaymentException;
import com.gzs.learn.payment.model.cfg.PaycfgChannel;
import com.gzs.learn.payment.model.cfg.TokenYinlian;
import com.gzs.learn.payment.model.unionpay.pay.UnionpayRequest;
import com.gzs.learn.payment.model.unionpay.query.UnionpayQuery;
import com.gzs.learn.payment.model.unionpay.query.UnionpayQueryResult;
import com.gzs.learn.payment.service.api.IUnionpayWapService;
import com.gzs.learn.payment.util.SignUtil;
import org.apache.http.NameValuePair;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.gzs.learn.common.util.HttpUtil.doPost;
import static com.gzs.learn.common.util.HttpUtil.obj2Pair;
import static com.gzs.learn.payment.util.AssertUtil.checkBlankStr;
import static com.gzs.learn.payment.util.AssertUtil.checkNumberPositive;
import static com.gzs.learn.payment.util.CommonsUtil.convertToken;
import static com.gzs.learn.payment.util.CommonsUtil.httpResultToObj;



@Component
public class UnionpayWapServiceImpl extends BaseUnionpay implements IUnionpayWapService {
    @Override
    public UnionpayRequest createPayInfo(UnionpayRequest unionpayRequest, PaycfgChannel paycfgChannel) {
        checkBlankStr(unionpayRequest.getOrderId(), "orderId can not be blank");
        checkBlankStr(unionpayRequest.getTxnTime(), "txtTime can not be blank");
        checkNumberPositive(unionpayRequest.getTxnAmt(), "txtAmt can is illegal");
        checkBlankStr(unionpayRequest.getBackUrl(), "backurl can not be blank");

        // 校验paycfgPla参数
        TokenYinlian tokenYinlian = convertToken(paycfgChannel.getToken(), TokenYinlian.class);
        checkToken(tokenYinlian);

        // 写入固定的的参数
        unionpayRequest.setVersion("5.0.0");
        unionpayRequest.setEncoding("UTF-8");
        unionpayRequest.setTxnType("01");
        unionpayRequest.setTxnSubType("01");
        unionpayRequest.setBizType("000201");
        unionpayRequest.setChannelType("08");
        unionpayRequest.setAccessType("0");
        unionpayRequest.setCurrencyCode("156");
        unionpayRequest.setMerId(tokenYinlian.getMerId());
        unionpayRequest.setCertId(tokenYinlian.getCertid());
        unionpayRequest.setSignMethod("01");
        String sign = SignUtil.yinlianSign(unionpayRequest, tokenYinlian);
        unionpayRequest.setSignature(sign);
        return unionpayRequest;
    }

    @Override
    public UnionpayQueryResult unionpayQuery(UnionpayQuery unionpayQuery, PaycfgChannel paycfgChannel) {
        // yinlianPay
        checkBlankStr(unionpayQuery.getOrderId(), "orderId can not be blank");
        checkBlankStr(unionpayQuery.getTxnTime(), "txtTime can not be blank");

        // 校验paycfgPla参数
        TokenYinlian tokenYinlian = convertToken(paycfgChannel.getToken(), TokenYinlian.class);
        checkToken(tokenYinlian);

        // 写入固定的的参数
        unionpayQuery.setVersion("5.0.0");
        unionpayQuery.setEncoding("UTF-8");
        unionpayQuery.setTxnType("00");
        unionpayQuery.setTxnSubType("00");
        unionpayQuery.setBizType("000201");
        unionpayQuery.setAccessType("0");
        unionpayQuery.setMerId(tokenYinlian.getMerId());
        unionpayQuery.setCertId(tokenYinlian.getCertid());
        unionpayQuery.setSignMethod("01");
        String sign = SignUtil.yinlianSign(unionpayQuery, tokenYinlian);
        unionpayQuery.setSignature(sign);

        List<NameValuePair> listParam = obj2Pair(unionpayQuery);
        Response<String> resp = doPost(YINLIAN_TRANS, listParam, String.class);
        UnionpayQueryResult result = httpResultToObj(resp.getEntity(), UnionpayQueryResult.class);
        checkBlankStr(result.getSignature(), "signature is null,pay request fail");
        if (!SignUtil.yinlianSignVerify(result, tokenYinlian)) {
            throw new PaymentException("signature is error");
        }
        return result;
    }
}
